<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('消息中心')" />
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-primary" onclick="readAll()">
                全部已读
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
    var statusDatas = [[${@dict.getType('sc_msg_status')}]];
    var prefix = ctx + "msg";

    $(function() {
        var options = {
            url: prefix + "/list",
            detailUrl: prefix + "/detail/{id}",
            modalName: "消息中心",
            columns: [
                {
                    field: 'id',
                    title: 'null',
                    visible: false
                },
                {
                    field: 'content',
                    title: '内容',
                    width: '80%'
                },
                {
                    field: 'status',
                    title: '状态',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(statusDatas, value);
                    }
                },
                {
                    field: 'createTime',
                    title: '时间',
                }],
            onClickRow:function (row, obj) {
                if(row.status == 0){
                    read(row.id);
                }

                if(row.msgType == 1){
                    //$.modal.openTab("消息",row.msgLink);
                }
                if(row.msgType == 2){
                    $.modal.openTab("学生请假列表",row.msgLink);
                }
                if(row.msgType == 3){
                    $.modal.openTab("调课申请",row.msgLink);
                }
                if(row.msgType == 4){
                    $.modal.openTab("教师请假列表",row.msgLink);
                }

            }
        };
        $.table.init(options);
    });

    function readAll() {

        $.ajax({
            type: "post",
            url:ctx + "school/msg/readAll",
            dataType: "json",
            success: function(res){
                updateMsg(1);
            }
        });
    }

    function read(id) {
        $.ajax({
            type: "post",
            url:ctx + "school/msg/read?id="+id,
            dataType: "json",
            success: function(res){
                updateMsg(2);
            }
        })
    }
    function updateMsg(type) {
        $.table.refresh();
        if(type == 1){
            $("#msgCount",parent.document).hide();
        }
        if(type == 2){
            var count = $("#msgCount",parent.document).text();
            count = Number(count)-1;
            if(count <= 0){
                $("#msgCount",parent.document).hide();
            }else {
                $("#msgCount",parent.document).text(count > 0 ? count : "");
            }
        }

    }
</script>
</body>
</html>